package com.nq.utils.stock;


import com.nq.pojo.SiteSetting;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 股票买卖日期
 */
@Component
public class BuyAndSellUtils {

    private static final Logger log = LoggerFactory.getLogger(BuyAndSellUtils.class);

    /**
     * 判断是否在美国股市交易时间内
     * @return
     */
    public static boolean UsStockMarketHours(String market, SiteSetting siteSetting){
        log.info("判断是否在股市交易时间内, market: " + market);
        try {
            if("HOSEIN".equalsIgnoreCase(market) || "HNX".equalsIgnoreCase(market) || "UPCOM".equalsIgnoreCase(market)){
                String am_begin = siteSetting.getTransAmBegin();
                String am_end = siteSetting.getTransAmEnd();
                String pm_begin = siteSetting.getTransPmBegin();
                String pm_end = siteSetting.getTransPmEnd();
                boolean am_flag = isTransTime(am_begin, am_end);
                boolean pm_flag = isTransTime(pm_begin, pm_end);
                log.info("【中国股】是否在上午交易 = {}，是否在下午交易 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
                if (!am_flag && !pm_flag) {
                    return false;
                }
                return true;
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.error("判断是否在股市交易时间内失败!");
        }

        return false;
    }


    public static boolean isTransTime(String begin_time, String end_time) throws Exception {
        if (StringUtils.isBlank(begin_time) || StringUtils.isBlank(end_time)) {
            return false;
        }
        Date nowDate = new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            SimpleDateFormat df2 = new SimpleDateFormat("HH:mm");
            now = df2.parse(df.format(nowDate));
            beginTime = df2.parse(begin_time);
            endTime = df2.parse(end_time);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Boolean flag = Boolean.valueOf(belongCalendar(now, beginTime, endTime));
        return flag.booleanValue();
    }

    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        //開始時間小於結束時間，正常情況
        if (begin.before(end)){
            if (date.after(begin) && date.before(end)) {
                return true;
            }
        }
        //開始時間大於結束時間，非正常情況
        if (begin.after(end)){
            if (date.after(begin) || date.before(end)) {
                return true;
            }
        }

        return false;
    }


    public static boolean isWorkDay(Date currentDate) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        if (cal.get(7) == 7 || cal.get(7) == 1) {
            return false;
        }
        return true;
    }


}
